﻿using System;
using Ease.EaseMission.Runtime;
using Ease.EaseNode.Editor.NodeView;
using Ease.EaseNode.Runtime;
using UnityEngine.UIElements;

namespace Ease.EaseMission.Editor.Node
{
    public class MissionStageNode : BaseMissionNode<MissionStage>, IOnNodeEditor
    {
        private Button btnEditor;
        private TextField textFieldName;
        public event Action<BaseEaseNode> OnNodeEditor;

        public MissionStageNode(IEaseNodeDataState state) : base(state)
        {
            textFieldName = new TextField();
            textFieldName.RegisterCallback<ChangeEvent<string>>(OnTextFieldNameChange);
            contentContainer.Add(textFieldName);
            textFieldName.SetValueWithoutNotify(missionData?.Name);
            btnEditor = new Button();
            btnEditor.text = "编辑";
            btnEditor.clicked += OnClickEditor;
            contentContainer.Add(btnEditor);
        }

        public override void OnNodeRemove()
        {
            base.OnNodeRemove();
            btnEditor.clicked -= OnClickEditor;
            OnNodeEditor = null;
        }

        protected override void OnRefreshAll()
        {
            base.OnRefreshAll();
            textFieldName.SetValueWithoutNotify(missionData?.Name);
            title = missionData?.Tip;
        }

        private void OnTextFieldNameChange(ChangeEvent<string> a)
        {
            if (missionData == null)
                return;
            missionData.Name = a.newValue;
        }

        private void OnClickEditor()
        {
            OnNodeEditor?.Invoke(this);
        }
    }
}